package com.ruoyi.web.controller.set;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.SetArea;
import com.ruoyi.system.service.ISetAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 区域Controller
 *
 * @author ruoyi
 * @date 2022-11-21
 */
@RestController
@Api(tags = "区域信息")
@RequestMapping("/set/area")
public class SetAreaController extends BaseController
{
    @Autowired
    private ISetAreaService setAreaService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询区域列表
     */
    @ApiOperation(value = "查询区域列表")
    @GetMapping("/list")
    public TableDataInfo list(@ApiParam(value = "区域参数", required = false) SetArea setArea)
    {
        startPage();
        List<SetArea> list = setAreaService.selectSetAreaList(setArea);
        return getDataTable(list);
    }

    /**
     * 查询区域下拉接口
     */
    @ApiOperation(value = "查询区域下拉接口")
    @GetMapping("/interface")
    public AjaxResult interfaces(@ApiParam(value = "区域参数", required = false) SetArea setArea)
    {
        List<SetArea> list = setAreaService.selectSetAreaList(setArea);
        return AjaxResult.success(list);
    }

    /**
     * 导出区域列表
     */
    @Log(title = "区域", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "导出区域列表")
    @GetMapping("/export")
    public AjaxResult export(HttpServletResponse response, @ApiParam(value = "区域参数", required = false) SetArea setArea)
    {
        List<SetArea> list = setAreaService.selectSetAreaList(setArea);
        ExcelUtil<SetArea> util = new ExcelUtil<SetArea>(SetArea.class);
        return util.exportExcel( list, "区域数据");
    }

    /**
     * 导入区域列表
     */
    @Log(title = "区域", businessType = BusinessType.IMPORT)
    @ApiOperation(value = "导入区域列表")
    @PostMapping("/importData")
    public AjaxResult importData(@ApiParam(value = "导入文件", required = true) MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SetArea> util = new ExcelUtil<SetArea>(SetArea.class);
        List<SetArea> areaList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = setAreaService.importArea(areaList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 导入模板
     */
    @ApiOperation(value = "导入模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        List<SetArea> list = setAreaService.selectSetAreaList(new SetArea());
        ExcelUtil<SetArea> util = new ExcelUtil<SetArea>(SetArea.class);
        return util.importTemplateExcel(list,"区域数据");
    }

    /**
     * 获取区域详细信息
     */
    @ApiOperation(value = "获取区域详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam(value = "区域id", required = true) @PathVariable("id") Long id)
    {
        return AjaxResult.success(setAreaService.selectSetAreaById(id));
    }

    /**
     * 新增区域
     */
    @Log(title = "区域", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增区域")
    @PostMapping
    public AjaxResult add(@ApiParam(value = "区域实体类", required = true) @RequestBody SetArea setArea)
    {
        if (UserConstants.NOT_UNIQUE.equals(setAreaService.checkCodeUnique(setArea)))
        {
            return AjaxResult.error("新增区域'" + setArea.getAreaName() + "'失败，区域编码已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(setAreaService.checkNameUnique(setArea)))
        {
            return AjaxResult.error("新增区域'" + setArea.getAreaName() + "'失败，区域名称已存在");
        }
        return toAjax(setAreaService.insertSetArea(setArea));
    }

    /**
     * 修改区域
     */
    @Log(title = "区域", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改区域")
    @PutMapping
    public AjaxResult edit(@ApiParam(value = "区域实体类", required = true) @RequestBody SetArea setArea)
    {
        if (UserConstants.NOT_UNIQUE.equals(setAreaService.checkCodeUnique(setArea)))
        {
            return AjaxResult.error("修改区域'" + setArea.getAreaName() + "'失败，区域编码已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(setAreaService.checkNameUnique(setArea)))
        {
            return AjaxResult.error("修改区域'" + setArea.getAreaName() + "'失败，区域名称已存在");
        }
        return toAjax(setAreaService.updateSetArea(setArea));
    }

    /**
     * 删除区域
     */
    @Log(title = "区域", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除区域")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@ApiParam(value = "区域id数组", required = true) @PathVariable Long[] ids)
    {
        return toAjax(setAreaService.deleteSetAreaByIds(ids));
    }
}
